package com.cn.jsdr.common;

import java.util.*;

public class Mytest {
    public static void main(String[] args) {
        Long ls=System.currentTimeMillis();
        selectSql();
        updateSql();
        insertSql();
        deleteSql();
        insertSqls();
        batchSqls();
        logic();
        Long l=System.currentTimeMillis();
        System.out.println(l-ls+"--------------------------");
    }
    @SuppressWarnings("rawtypes")
    public static Map myLogic(String[] sqlids, Map param, Map sqlP) {
        // Map resMap=new HashMap();
        Map allMap = new HashMap();
        // allMap.putAll((Map) param.get("pub"));
        for (String sqlid : sqlids) {//循环sqlid
            /**
             * param 有三类
             * 公共的  公用id                 默认为pub
             * 私有的  每个sql需要的参数      默认为hand开头
             * 返回的  sql执行结果返回的数据  默认为ret开头
             */
            Map exe = new HashMap(); //可执行的 包含三部分  公共  私有  返回
            //  exe.putAll((Map) param.get("pub"));
            //   exe.putAll(resMap);  //返回的数据放入
            //exe.putAll(redisUtil.hmget(param.get("token")+""));//这个是公共的一部分   如 token  userid  roleid  orgid等
            //  Map<String, String> sql = redisService.getSqlSplit(sqlid);//redis 读取; //获取sql片段  和指定有关的参数 名称
            Map sql = (Map) sqlP.get(sqlid);//redis 读取; //获取sql片段  和指定有关的参数 名称
            String keyword =sql.get("keyword")+"";
            if(keyword==null||"".equals(keyword)){
                throw new RuntimeException("【关键字】不能为空！");
            }
            String handler = sql.get("handler")+"";// 获取指定的参数名称
            if(handler==null||"".equals(handler)){
                throw new RuntimeException("【参数句柄】不能为空！");
            }
            // Map<String,Object> sqlParam = (Map<String, Object>) param.get(handler);
            exe.putAll((Map)param.get(handler));//   获取当前sql片段的参数
            allMap.put("hand"+sqlid,(Map)param.get(handler));
            if("update".equalsIgnoreCase(keyword)){
                String exeSql=SqlConfig.getUpdateSql(sql,exe);
                // int n =jdbcTemplate.update(exeSql);
                // allMap.put("res"+sqlid,n);
                System.out.println(exeSql);
                continue;
            }
            if("insert".equalsIgnoreCase(keyword)){
                String exeSql=SqlConfig.getAddSql(sql, exe);
                //int n = jdbcTemplate.update(exeSql);
                //  allMap.put("res"+sqlid,n);
                System.out.println(exeSql);
                continue;
            }
            if("delete".equalsIgnoreCase(keyword)){
                String exeSql=SqlConfig.getDeletesql(sql,exe);
                //int n = jdbcTemplate.update(exeSql);
                //allMap.put("res"+sqlid,n);
                System.out.println(exeSql);
                continue;
            }
            if("select".equalsIgnoreCase(keyword)){
                String exeSql= SqlConfig.getQuerySql(sql,exe);
                //      resMap=null;
                System.out.println(exeSql);
                //  resMap =jdbcTemplate.queryForMap(exeSql);
                //   allMap.put("res"+sqlid,resMap);
                continue;
            }
        }
        return allMap;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Map logic() {
        Map sqlslices =new HashMap<String ,Object>();
        Map sqlslice =new HashMap<String ,Object>();
        sqlslice.put("handler", "hselect");///////  标识 要调用那个参数
        sqlslice.put("keyword", "select");
        sqlslice.put("mtable", "a.carteid,a.cartename from dcblm.carte a ");
        sqlslice.put("joinon", "");
        sqlslice.put("statecond", "where ");
        sqlslice.put("dynconds", "carteid#a.partid=##6@cartename#and a.cartename like '#%'#0@" +
                "carteurl#and a.carteurl='#'#0@id#and a.id in(#)#0");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "limit#offset#10#0");
        sqlslice.put("sort", "");
        sqlslices.put("select", sqlslice);
        Map<String ,String> sqlsliceu =new HashMap<String ,String>();
        sqlsliceu.put("handler", "hupdate");///////  标识 要调用那个参数
        sqlsliceu.put("keyword", "update");
        sqlsliceu.put("mtable", "dcblm.carte set ");
        sqlsliceu.put("joinon", "carteid#a.partid='#'#4@cartename1#, a.cartename1=##2");
        sqlsliceu.put("statecond", "where a.delete =1");
        sqlsliceu.put("dynconds", "carteid#and a.partid=##0@cartename#and a.cartename like '#%'#0@" +
                "carteurl#and a.carteurl='#'#0@id#and a.id in(#)#0");
        sqlsliceu.put("ending", "");
        sqlsliceu.put("paging", "index");
        sqlsliceu.put("sort", "");
        sqlslices.put("update", sqlsliceu);
        Map<String,Object> params =new HashMap<String ,Object>();
        Map<String,Object> param =new HashMap<String ,Object>();
        param.put("carteid", 3);
        param.put("cartes", "cartes");
        param.put("id", "'1','3','4'");
        param.put("cartename", "添加菜单");
        param.put("pageindex", 5);
        param.put("pagesize", 2);
        param.put("carteurl", "httn");
        params.put("hupdate", param);//////  指定那个sql调用拼接我的参数
        Map<String,Object> paramu =new HashMap<String ,Object>();
        paramu.put("carteid", 5);
        paramu.put("cartes", "cartes");
        paramu.put("id", "'1','3','4'");
        paramu.put("cartename", "添加菜单");
        paramu.put("pageindex", 5);
        paramu.put("pagesize", 2);
        paramu.put("carteurl", "httn");
        params.put("hselect", paramu);///////// 指定那个sql调用拼接我的参数
        String[] sqlids={"select","update"};
        Map m =myLogic(sqlids, params, sqlslices);
        System.out.println(m);
        return m;
    }
    public static  void selectSql(){
        Long l=System.currentTimeMillis();
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "select");
        sqlslice.put("mtable", "a.carteid,a.cartename from dcblm.carte a ");
        sqlslice.put("joinon", "");
        sqlslice.put("statecond", "where ");
        sqlslice.put("dynconds", "carteid#a.partid=##6@cartename#and a.cartename like '#%'#0@" +
                "carteurl#and a.carteurl='#'#0@id#and a.id in(#)#0");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "limit#offset#10#0");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();
        param.put("carteid", 3);
        param.put("id", "'1','3','4'");
        param.put("cartename", "添加菜单");
        param.put("pageindex", 1);
        param.put("pagesize", 2);
        param.put("carteurl", "httn");
        String sql =SqlConfig.getQuerySql(sqlslice,param);
        Long ls=System.currentTimeMillis();
        System.out.println(ls-l+"--XXXXXXXXXXXXXXX");
        System.out.println(sql);
    }


    public static  void updateSql(){
        Long l=System.currentTimeMillis();
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "update");
        sqlslice.put("mtable", "dcblm.carte set ");
        sqlslice.put("joinon", "carteid#a.partid='#'#4@cartename1#, a.cartename1=##2");
        sqlslice.put("statecond", "where a.delete =1");
        sqlslice.put("dynconds", "carteid#and a.partid=##0@cartename#and a.cartename like '#%'#0@" +
                "carteurl#and a.carteurl='#'#0@id#and a.id in(#)#0");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "index");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();
        param.put("carteid", "3");
        param.put("cartename1",21);
        param.put("id", "'1','3','4'");
        param.put("cartename", "添加菜单");
        param.put("index", "2");
        param.put("page", 2);
        param.put("carteurl", "httn");
        String sql = SqlConfig.getUpdateSql(sqlslice,param);
        Long ls=System.currentTimeMillis();
        System.out.println(ls-l+"--XXXXXXXXXXXXXXX");
        System.out.println(sql);
    }

    public static  void deleteSql(){
        Long l=System.currentTimeMillis();
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "delete");
        sqlslice.put("mtable", "from dcblm.carte ");
        //sqlslice.put("joinon", "carteid#,a.partid='#'#F#F#F@cartename1#, a.cartename1=##F#T#F");
        sqlslice.put("statecond", "where a.delete =1");
        sqlslice.put("dynconds", "carteid#and a.partid=##0@cartename#and a.cartename like '#%'#0@" +
                "carteurl#and a.carteurl='#'#0@id#and a.id in(#)#0");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "index");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();
        param.put("carteid", "3");
        param.put("cartename1",21);
        param.put("id", "'1','3','4'");
        param.put("cartename", "添加菜单");
        param.put("index", "2");
        param.put("page", 2);
        param.put("carteurl", "httn");
        String sql =SqlConfig.getDeletesql(sqlslice,param);
        Long ls=System.currentTimeMillis();
        System.out.println(ls-l+"--XXXXXXXXXXXXXXX");
        System.out.println(sql);
    }

    public static void insertSql(){
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "insert");
        sqlslice.put("mtable", "into dcblm.carte ");
        //sqlslice.put("joinon", "carteid#,a.partid='#'#F#F#F@cartename1#, a.cartename1=##F#T#F");
        sqlslice.put("statecond", "where a.delete =1");
        sqlslice.put("dynconds", "carteid#carteidt##0@cartename#cartename##0@" +
                "carteurl#carteurl##0@id#ids##2");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "index");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();
        param.put("carteid",3 );
        param.put("id", 345);
        param.put("cartename1",21);
        //param.put("id", "134");
        param.put("cartename", "添加菜单");
        param.put("index", "2");
        param.put("page", 2);
        param.put("carteurl", "httn=1 and a=");
        String sql =SqlConfig.getAddSql(sqlslice,param);
        System.out.println(sql);
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void insertSqls(){
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "insert");
        sqlslice.put("mtable", "into dcblm.carte ");
        //sqlslice.put("joinon", "carteid#,a.partid='#'#F#F#F@cartename1#, a.cartename1=##F#T#F");
        sqlslice.put("statecond", "where a.delete =1");
        sqlslice.put("dynconds", "carteid#carteidt##0@cartename#cartename##0@" +
                "carteurl#carteurl##0@id#ids##1");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "index");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();

        param.put("carteid", "3");
        param.put("cartename1",21);
        //param.put("id", "134");
        param.put("cartename", "添加菜单");
        param.put("index", "2");
        param.put("page", 2);
        param.put("carteurl", "httn");
        List list = new ArrayList();
        list.add(param);
        Map<String,Object> param2 =new HashMap<String ,Object>();

        param2.put("carteid", "3");
        param2.put("cartename1",21);
        //param.put("id", "134");
        param2.put("cartename", "添加菜单");
        param2.put("index", "2");
        param2.put("page", 2);
        param2.put("carteurl", "httn");
        list.add(param2);
        Map<String,Object> param3 =new HashMap<String ,Object>();

        param3.put("carteid", "3");
        param3.put("cartename1",21);
        //param.put("id", "134");
        param3.put("cartename", "添加菜单");
        param3.put("index", "2");
        param3.put("page", 2);
        param3.put("carteurl", "httn");
        list.add(param3);

        String sql  =SqlConfig.getAddSqls(sqlslice, list);
        System.out.println(sql);
    }
    @SuppressWarnings("unchecked")
    public static void batchSqls(){
        Map<String ,String> sqlslice =new HashMap<String ,String>();
        sqlslice.put("keyword", "insert");
        sqlslice.put("mtable", "into dcblm.carte ");
        //sqlslice.put("joinon", "carteid#,a.partid='#'#F#F#F@cartename1#, a.cartename1=##F#T#F");
        sqlslice.put("statecond", "where a.delete =1");
        sqlslice.put("dynconds", "carteid#carteidt##0@cartename#cartename##0@" +
                "carteurl#carteurl##0@id#ids##1");
        sqlslice.put("ending", "");
        sqlslice.put("paging", "index");
        sqlslice.put("sort", "");
        Map<String,Object> param =new HashMap<String ,Object>();

        param.put("carteid", "3");
        param.put("cartename1",21);
        //param.put("id", "134");
        param.put("cartename", "添加菜单");
        param.put("index", "2");
        param.put("page", 2);
        param.put("carteurl", "httn");
        List list = new ArrayList();
        list.add(param);
        Map<String,Object> param2 =new HashMap<String ,Object>();

        param2.put("carteid", "3");
        param2.put("cartename1",21);
        //param.put("id", "134");
        param2.put("cartename", "添加菜单");
        param2.put("index", "2");
        param2.put("page", 2);
        param2.put("carteurl", "httn");
        list.add(param2);
        Map<String,Object> param3 =new HashMap<String ,Object>();

        param3.put("carteid", "3");
        param3.put("cartename1",21);
        //param.put("id", "134");
        param3.put("cartename", "添加菜单");
        param3.put("index", "2");
        param3.put("page", 2);
        param3.put("carteurl", "httn");
        list.add(param3);
        Map<String,Object> param4=new HashMap<String ,Object>();
        param.putAll(param3);
        list.add(param4);
        Map<String,Object> param5=new HashMap<String ,Object>();
        param5.putAll(param3);
        list.add(param5);
        Map<String,Object> param6=new HashMap<String ,Object>();
        param6.putAll(param3);
        list.add(param6);
        Map<String,Object> param7=new HashMap<String ,Object>();
        param7.putAll(param3);
        list.add(param7);
        Map<String,Object> param8=new HashMap<String ,Object>();
        param8.putAll(param3);
        list.add(param8);
        Map<String,Object> param9=new HashMap<String ,Object>();
        param9.putAll(param3);
        list.add(param9);

        String sql[] =SqlConfig.getBatchSqls(sqlslice, list);
        System.out.println(Arrays.toString(sql));
    }
}
